package com.start.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.admin.entity.StartSysUser;
import com.start.admin.mappers.SysUserMapper;
import com.start.admin.service.StartSysUserService;
import com.start.common.constant.StartConstant;
import com.start.common.details.BaseUserDetails;
import com.start.common.exceptions.StartBusException;
import com.start.core.vo.BaseUpdatePassword;
import com.start.core.vo.UpdateCurrentUserPasswordVo;
import com.start.security.StartTokenTools;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class StartSysUserServiceImpl extends ServiceImpl<SysUserMapper, StartSysUser> implements StartSysUserService {


    @Override
    public StartSysUser getByUserName(String userName) {
        LambdaQueryWrapper<StartSysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StartSysUser::getUserName, userName);
        return getOne(lambdaQueryWrapper);
    }

    @Override
    public void createUser(StartSysUser entity) {
        if (StringUtils.isAllBlank(entity.getUserName())) throw new StartBusException("账号不能为空！");
        if (StringUtils.isAllBlank(entity.getUserPwd())) throw new StartBusException("用户密码不能为空!");
        if (entity.getUserPwd().length() < 5) throw new StartBusException("用户密码最少6个字符");
        entity.setUserPwd(StartConstant.passwordEncoder.encode(entity.getUserPwd()));
        save(entity);
    }

    @Override
    public void updateUserPassword(BaseUpdatePassword model) {
        if (StringUtils.isAllBlank(model.getUserId())) throw new StartBusException("用户id不能为空");
        if (StringUtils.isAllBlank(model.getPassword())) throw new StartBusException("用户密码不能为空");
        if (model.getPassword().length() < 5) throw new StartBusException("用户密码最少6个字符");
        StartSysUser user = getById(model.getUserId());
        if (user == null) throw new StartBusException("用户不存在");
        String encode = StartConstant.passwordEncoder.encode(model.getPassword());
        user.setUserPwd(encode);
        updateById(user);
    }

    @Override
    public void updateCurrentUserPassword(UpdateCurrentUserPasswordVo model) {
        BaseUserDetails currentUser = StartTokenTools.getCurrentUser();
        if (currentUser == null) throw new StartBusException("当前用户未登录请登录后再试！");
        model.setUserId(currentUser.getUserId());
        if (StringUtils.isEmpty(model.getPassword())) throw new StartBusException("用户密码不能为空！");
        if (StringUtils.isEmpty(model.getRePassword())) throw new StartBusException("确认密码不能为空！");
        if (model.getPassword().equals(model.getRePassword())) throw new StartBusException("两次输入密码不相同！");
        StartSysUser user = getById(model.getUserId());
        if (user == null) throw new StartBusException("用户不存在");
        if (!StartConstant.passwordEncoder.matches(model.getPassword(), user.getUserPwd()))
            throw new StartBusException("用户密码不正确！");
        user.setUserPwd(StartConstant.passwordEncoder.encode(model.getPassword()));
        updateById(user);
    }

    @Override
    public void updateUserLockedState(String userId, Integer state) {
        StartSysUser user = checkedUserState(userId, state);
        switch (state) {
            case 0:
            case 1:
                break;
            default:
                throw new StartBusException("未知用户状态");
        }
        user.setIsLocked(state);
        updateById(user);
    }

    private StartSysUser checkedUserState(String userId, Integer state) {
        StartSysUser user = getById(userId);
        if (user == null) throw new StartBusException("用户不存在！");
        if (state == null) throw new StartBusException("未知用户状态！");
        return user;
    }

    @Override
    public void updateUserEnabledState(String userId, Integer state) {
        StartSysUser user = checkedUserState(userId, state);
        switch (state) {
            case 0:
            case 1:
                break;
            default:
                throw new StartBusException("未知用户状态");
        }
        user.setIsLocked(state);
        updateById(user);
    }


}
